package org.ysling.litemall.core.task;
// Copyright (c) [ysling] [927069313@qq.com]
// [litemall-plus] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//             http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ysling.litemall.core.service.CouponVerifyService;
import org.ysling.litemall.core.system.SystemConfig;
import org.ysling.litemall.core.task.service.Task;
import org.ysling.litemall.core.util.BeanUtil;
import org.ysling.litemall.db.domain.LitemallGroupon;
import org.ysling.litemall.db.domain.LitemallOrder;
import org.ysling.litemall.db.domain.LitemallOrderGoods;
import org.ysling.litemall.db.service.LitemallGoodsProductService;
import org.ysling.litemall.db.service.LitemallGrouponService;
import org.ysling.litemall.db.service.LitemallOrderGoodsService;
import org.ysling.litemall.db.service.LitemallOrderService;
import org.ysling.litemall.db.util.GrouponConstant;
import org.ysling.litemall.db.util.OrderConstant;

import java.util.List;
import java.util.Objects;

/**
 * 订单超时支付延时队列
 */
public class OrderUnpaidTask extends Task {

    private final Log logger = LogFactory.getLog(OrderUnpaidTask.class);
    private int orderId = -1;

    public OrderUnpaidTask(Integer orderId, long delayInMilliseconds){
        super("OrderUnpaidTask-" + orderId, delayInMilliseconds);
        this.orderId = orderId;
    }

    public OrderUnpaidTask(Integer orderId){
        super("OrderUnpaidTask-" + orderId, SystemConfig.getOrderUnpaid() * 60 * 1000);
        this.orderId = orderId;
    }

    @Override
    public void run() {
        logger.info("系统开始处理延时任务---订单超时未付款---" + this.orderId);
        LitemallOrderService orderService = BeanUtil.getBean(LitemallOrderService.class);
        LitemallOrderGoodsService orderGoodsService = BeanUtil.getBean(LitemallOrderGoodsService.class);
        LitemallGrouponService grouponService = BeanUtil.getBean(LitemallGrouponService.class);
        LitemallGoodsProductService productService = BeanUtil.getBean(LitemallGoodsProductService.class);
        CouponVerifyService couponVerifyService = BeanUtil.getBean(CouponVerifyService.class);

        LitemallOrder order = orderService.findById(this.orderId);
        if(order == null || !OrderConstant.isCreateStatus(order)) return;

        // 设置订单已取消状态
        order.setOrderStatus(OrderConstant.STATUS_AUTO_CANCEL);
        if (orderService.updateVersionSelective(order) == 0) {
            throw new RuntimeException("更新数据已失效");
        }

        // 商品货品数量增加
        Integer orderId = order.getId();
        List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
        for (LitemallOrderGoods orderGoods : orderGoodsList) {
            Integer productId = orderGoods.getProductId();
            Short number = orderGoods.getNumber();
            if (productService.addStock(productId, number) == 0) {
                throw new RuntimeException("商品货品库存增加失败");
            }
        }

        //设置团购取消状态
        LitemallGroupon grouponSource = grouponService.queryByOrderId(orderId);
        if (!Objects.isNull(grouponSource)){
            grouponSource.setStatus(GrouponConstant.STATUS_CANCEL);
            if (grouponService.updateVersionSelective(grouponSource) == 0) {
                throw new RuntimeException("团购取消失败");
            }
        }

        //返还优惠券
        couponVerifyService.releaseCoupon(orderId);
        logger.info("系统结束处理延时任务---订单超时未付款---" + this.orderId);
    }
}
